import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    timeOut: null,
    delBtnShow: true,
    array: [],
    index: null,
    type: 'add',
    form: {
      uniqueCode: '',
      stepName: '',
      stepCode: '',
      startTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      outPut: '',
      checkerId: ''
    }
  },

  onLoad(query) {
    if (query.type == 'add') {
      this.getMateDataRecordQuery()
      this.setData({
        delBtnShow: false
      })
      dd.setNavigationBar({
        title: '新增质检信息'
      })
    }
    if (query.type == 'edit') {
      this.qualityControlQuery(query)
      dd.setNavigationBar({
        title: '编辑质检信息'
      })
      this.setData({
        delBtnShow: true
      })
    }

    this.setData({
      type: query.type
    })
  },

  onShow() {
    if (this.data.getSelectTechnics) {
      console.log(this.data.selectTechnicsData)
      this.setData({
        "form.stepName": this.data.selectTechnicsData[0].title,
        "form.stepCode": this.data.selectTechnicsData[0].value
      })
      this.qualityControlOutput()
    }
  },

  // 工艺步骤跳转
  toSelRange(e) {
    if (this.data.type == 'edit') {
      return
    }
    dd.navigateTo({
      url: '/pages/selectTechnics/selectTechnics?type=back'
    })
  },

  // 检验时间或结束时间选择
  selProData(e) {
    let that = this
    let prop = e.target.dataset.prop;
    let currentDate = e.target.dataset.date
    dd.datePicker({
      format: 'yyyy-MM-dd HH:mm:ss',
      currentDate: currentDate,
      success: (res) => {
        console.log(res)
        if (prop == 'start') {
          that.setData({
            "form.startTime": res.date
          })
        } else {
          that.setData({
            "form.endTime": res.date
          })
        }
      },
    });
  },

  // 检验员
  bindObjPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    let val = e.detail.value
    this.setData({
      index: val,
      'form.checkerId': this.data.array[val].dap_id_
    });
  },

  save() {
    if (this.data.delBtnShow) {
      this.update()
    } else {
      this.add()
    }
  },

  add() {
    let that = this
    $http(apiSetting.qualityControlAdd, {
      endTime: dayjs(that.data.form.endTime).format('YYYY-MM-DD HH:mm:ss'), //	质检结束时间
      outPut: that.data.form.outPut, //	质检总产量
      startTime: dayjs(that.data.form.startTime).format('YYYY-MM-DD HH:mm:ss'), //	质检开始时间
      stepCode: that.data.form.stepCode, //	工艺步骤编码
      uniqueCode: that.data.form.uniqueCode, //	唯一码
      checkerId: that.data.form.checkerId //	检验员ID
    }).then((data) => {
      if (data.code == 0) {
        dd.navigateBack({
          delta: 1
        })
      }
    })
  },

  update() {
    let that = this
    $http(apiSetting.qualityControlUpdate, {
      endTime: dayjs(that.data.form.endTime).format('YYYY-MM-DD HH:mm:ss'), //	质检结束时间
      outPut: that.data.form.outPut, //	质检总产量
      startTime: dayjs(that.data.form.startTime).format('YYYY-MM-DD HH:mm:ss'), //	质检开始时间
      stepCode: that.data.form.stepCode, //	工艺步骤编码
      uniqueCode: that.data.form.uniqueCode, //	唯一码
      checkerId: that.data.form.checkerId, //	检验员ID
      id: that.data.form.id,
    }).then((data) => {
      if (data.code == 0) {
        dd.navigateBack({
          delta: 2
        })
      }
    })
  },

  // 删除
  del() {
    let that = this
    dd.confirm({
      title: '温馨提示',
      content: '删除不可恢复，你确定要删除？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm == true) {
          $http(apiSetting.qualityControlDelete, {
            id: that.data.form.id
          }).then((data) => {
            if (data.code == 0) {
              dd.navigateBack({
                delta: 2
              })
            }
          })
        }
      },
    });
  },

  // 查询详情
  qualityControlQuery(query) {
    let that = this
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 3]; //上上一个页面
    let list = prevPage.data.list
    let find = list.find(n => {
      return n.id == query.id
    })
    this.getMateDataRecordQuery()
    console.log(find)
    let arr = find.stepTree.split('/')
    find.stepName = arr[arr.length - 1]
    that.setData({
      form: find
    })
  },

  // 人员
  getMateDataRecordQuery() {
    let that = this
    $http(apiSetting.mateDataRecordQuery, {
      metaDataName: "人员", //定值：人员 物料 生产设备
    }).then((data) => {
      that.setData({
        array: data.result
      })
      if (that.data.type == 'edit') {
        let find = data.result.findIndex(n => {
          return n.dap_id_ == that.data.form.checkerId
        })
        if (find != -1) {
          that.setData({
            index: find
          })
        }
      }
    })
  },

  onInputChange(e) {
    let that = this
    clearTimeout(that.data.timeOut)
    let type = e.target.dataset.type
    let value = e.detail.value
    that.data.form[type] = value
    that.data.timeOut = setTimeout(function () {
      that.qualityControlOutput()
    }, 1000);
  },

  // 总产量
  // onOutPut(e) {
  //   this.data.form.outPut = e.detail.value
  // },

  // 扫码唯一码
  scanUniqueCode() {
    let that = this
    dd.scan({
      type: 'qr',
      success: (res) => {
        that.setData({
          "form.uniqueCode": res.code
        })
        that.qualityControlOutput()
      },
    });
  },

  qualityControlOutput() {
    let that = this
    if (that.data.form.stepCode != '' && that.data.form.uniqueCode != '') {
      $http(apiSetting.qualityControlOutput, {
        stepCode: that.data.form.stepCode,
        uniqueCode: that.data.form.uniqueCode
      }).then((data) => {
        that.setData({
          "form.outPut": data.result
        })
      })
    }
  }
});
